<template>
  <div class="recipient-party">
    <el-select
      v-model="checkedData"
      filterable
      remote
      reserve-keyword
      :placeholder="parameter.promptTemplate"
      :remote-method="remoteMethod"
      :loading="loading"
      :disabled="readonly"
      value-key="code"
      clearable
      @change="changeData"
      @clear="remoteMethod('', 'clear')"
    >
      <el-option
        v-for="item in optionsList"
        :key="item.code"
        :label="item.code + item.fullName"
        :value="item"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
/**
 *  http://wiki.zczy56.com/pages/viewpage.action?pageId=50341610  客户
    http://wiki.zczy56.com/pages/viewpage.action?pageId=50346399 内部公司 tms的签约主体， wms的签约主体
    http://172.20.21.211:8000/#/view/m8wJ0eN2                租户下内部用户接口
 */
import masertDataService from '@/services/masertDataService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  name: 'recipientParty',
  props: {
    detail: {
      typr: Object,
      default: () => {
        return {}
      },
    },
    value: [Object, String],
    disabled: Boolean,
    parameter: {
      typr: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      checkedData: {},
      loading: false,
      optionsList: [],
      readonly: false,
      rules: {
        billSender: [
          {
            required: this.parameter.state === 4,
            validator: (rule, value, callback) => {
              if (this.parameter.state != 4 && this.parameter.fillFlag != 0) {
                callback()
                return
              }
              if (this.checkedData.code) {
                callback()
              } else {
                callback(new Error(this.$t('请选择收款方')))
                return
              }
            },
            trigger: 'change',
            type: 'array',
          },
        ],
      },
    }
  },
  created() {
    let vm = this
    if (this.value) {
      this.checkedData = JSON.parse(this.value)
      this.optionsList = [this.checkedData]
      this.remoteMethod(this.checkedData.code, '', true)
      this.$nextTick(() => {
        this.$emit('input', this.checkedData)
      })
    } else {
      this.remoteMethod('', '', true)
    }
    //接收客户主数据的第一次添加
    eventBus.$on('masterChange', (parameter, data) => {
      //必须是抬头信息的塞到抬头信息里面，支付条款的塞到支付条款里面
      if (
        !parameter.parameterPosition ||
        parameter.parameterPosition !== vm.parameter.parameterPosition
      ) {
        return
      }
      let noValue = !this.checkedData.code
      let templateTypeCode = this.detail.templateTypeCode
      if (noValue) {
        if (
          templateTypeCode === 'TMSHZ' ||
          templateTypeCode === 'WMSHZ2' ||
          templateTypeCode === 'WMSHZ1'
        ) {
          if (parameter.parameterNumber === 'signSubject') {
            this.checkedData = data
            this.optionsList = [this.checkedData]
          }
        } else {
          if (parameter.parameterNumber === 'signTarget') {
            this.checkedData = data
            this.optionsList = [this.checkedData]
          }
        }
        eventBus.$emit('changeaccount', this.parameter, this.checkedData)
      }
    })
    this.readonly = Boolean(this.$route.query.readonly) || this.disabled
    this.$emit('setrules', this.parameter, {
      [this.parameter.parameterNumber]: this.rules.billSender,
    })
  },
  methods: {
    remoteMethod(query, type, init) {
      //多式联运货主
      let kind = []
      let templateTypeCode = this.detail.templateTypeCode
      let systemFlag = null
      switch (templateTypeCode) {
        case 'TMSHZ': //多式联运货主
          kind = [1, 2]
          systemFlag = 1
          break
        case 'TMSCYS': //多式联运承运商
          kind = [3]
          systemFlag = 1
          break
        case 'TMSSJ': //多式联运个体司机
          kind = [4]
          systemFlag = 1
          break
        case 'TMSCLB': //多式联运车老板
          kind = [5]
          systemFlag = 1
          break
        case 'WMSHZ1': //仓储货主（通用）
          kind = [6]
          systemFlag = 2
          break
        case 'WMSHZ2': //仓储货主（行业）
          kind = [6]
          systemFlag = 2
          break
      }
      if (type === 'clear') {
        this.optionsList = []
        this.checkedData = {}
      } else {
        //只有TMS多式联运货主和wms的模板调签约主体接口，其他的调的客户接口
        if (
          templateTypeCode === 'TMSHZ' ||
          templateTypeCode === 'WMSHZ2' ||
          templateTypeCode === 'WMSHZ1'
        ) {
          this.remoteSignSubject(query, systemFlag, init)
        } else {
          this.remoteSignTarget(query, kind, init)
        }
      }
    },
    remoteSignTarget(query, kind, init) {
      masertDataService
        .findSaasAllCustomerList({
          kind: kind, //Tms: 1、货主  2、托运商  3、分供方,4 司机 5车老板(tms); wms: 6 wms货主(wms)
          status: [1],
          codeOrNameLike: query,
          dataPerFlag: 1,
        })
        .then((res) => {
          this.optionsList = res.data.map((it) => {
            return {
              ...it,
            }
          })
          if (init) {
            let data = this.optionsList.find(
              (it) => it.code === this.checkedData.code
            )
            if (data) {
              eventBus.$emit('changeaccount', this.parameter, data)
            }
          }
        })
    },
    remoteSignSubject(query, systemFlag, init) {
      masertDataService
        .findSaasAllCompanyList({
          state: 1,
          // systemFlag: systemFlag, //运输:1.运输,2.仓储,3.园区,4.商贸,5.核算系统,6.金融,7.价格中心,8.网货
          systemFlag: null,
          skipPrdFlag: 1, // 1不限产品范围  其他限制
          codeOrNameLike: query,
          tenantCode: localStorage.getItem('tenantCode'),
        })
        .then((res) => {
          this.optionsList = res.data.map((it) => {
            return {
              ...it,
              code: it.companyCode,
              name: it.companyName,
              fullName: it.companyFullName,
            }
          })
          if (init) {
            let data = this.optionsList.find(
              (it) => it.code === this.checkedData.code
            )
            if (data) {
              eventBus.$emit('changeaccount', this.parameter, data)
            }
          }
        })
    },
    changeData() {
      this.$emit('input', this.checkedData)
      eventBus.$emit('changeaccount', this.parameter, this.checkedData)
    },
    getData() {
      return {
        code: this.checkedData.code,
        name: this.checkedData.name,
        fullName: this.checkedData.fullName,
      }
    },
  },
}
</script>
<style lang="scss">
.recipient-party {
  .el-select {
    width: 100%;
  }
}
</style>
